# -*- coding: utf-8 -*-

from PyQt4.QtGui import QWidget
from PyQt4.QtCore import SIGNAL, QString
from PyQt4.Qt import QRect, QDesktopWidget
from ui_mainwindow import Ui_MainWindow
from ProjectSelect import ProjectSelect
from ProjectCreate import ProjectCreate
from subprocess import *
from mainfunctions import *
from cvssvn import *
from githg import *
import os

class MainWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.projCreate = ProjectCreate()
        self.projCreate.browser = self.ui.textBrowser
        self.projCreate.setSelectedVCS(self.ui.vcsListCBox.currentText())
        self.projCreate.setProjectRootUrl(str(self.ui.vcsRepoUrl.text()))
        self.projCreate.hide()
        
        self.projSelect = ProjectSelect()        
        self.projSelect.setBrowser(self.ui.textBrowser)
        self.projSelect.hide()
        
        available_geom = QDesktopWidget().availableGeometry()
        current_geom = self.frameGeometry()
        self.setGeometry(available_geom.width() / 2 - current_geom.width() / 2, available_geom.height() / 2 - current_geom.height() / 2, current_geom.width(), current_geom.height());


        self.connect(self.ui.checkOutStartBtn, SIGNAL("clicked()"), self.startCheckOut)
        self.connect(self.ui.checkOutStopBtn, SIGNAL("clicked()"), self.stopCheckOut)
	self.connect(self.ui.parseCommitLog, SIGNAL("clicked()"), self.parseCommitLog)	
        self.connect(self.ui.train_testCsvButton, SIGNAL("clicked()"), self.train_testCsvGeneration)
        self.connect(self.ui.defectMatchButton, SIGNAL("clicked()"), self.defect)
        self.connect(self.ui.predictButton, SIGNAL("clicked()"), self.predict)
        self.connect(self.ui.vcsRepoUrl, SIGNAL("textChanged(const QString &)"), self.urlChanged)
        self.connect(self.ui.vcsListCBox, SIGNAL("currentIndexChanged(const QString &)"), self.vcsChanged)

    def vcsChanged(self, currentCvs):
        self.projCreate.setSelectedVCS(str(currentCvs))

    def urlChanged(self, urlPath):
        self.projCreate.setProjectRootUrl(str(urlPath))
        
    def startCheckOut(self):
        if(self.ui.vcsRepoUrl.text() == ""):
            self.ui.textBrowser.clear()
            self.ui.textBrowser.append("Enter Server Url...")
            self.ui.vcsRepoUrl.setFocus()

        else:
            if(self.ui.vcsListCBox.currentText() == "CVS"):
                self.ui.checkOutStopBtn.setEnabled(True)
                self.ui.textBrowser.clear()
                self.ui.textBrowser.append("<<" + self.ui.vcsListCBox.currentText() + ">> will be used as the Version Control System...")
                getCvsRepo(str(self.ui.vcsRepoUrl.text()), "fooo", self.ui.textBrowser)
            
            elif(self.ui.vcsListCBox.currentText() == "Git"):
                self.ui.checkOutStopBtn.setEnabled(True)
                self.ui.textBrowser.clear()
                self.ui.textBrowser.append("<<" + self.ui.vcsListCBox.currentText() + ">> will be used as the Version Control System...")
                getGitRepo(str(self.ui.vcsRepoUrl.text()), "fooo", self.ui.textBrowser)

            elif(self.ui.vcsListCBox.currentText() == "Mercurial"):
                self.ui.checkOutStopBtn.setEnabled(True)
                self.ui.textBrowser.clear()
                self.ui.textBrowser.append("<<" + self.ui.vcsListCBox.currentText() + ">> will be used as the Version Control System...")
                getMercurialRepo(str(self.ui.vcsRepoUrl.text()), "fooo", self.ui.textBrowser)

            elif(self.ui.vcsListCBox.currentText() == "Subversion"):
                self.ui.checkOutStopBtn.setEnabled(True)
                self.ui.textBrowser.clear()
                self.ui.textBrowser.append("<<" + self.ui.vcsListCBox.currentText() + ">> will be used as the Version Control System...")
                getSubversionRepo(str(self.ui.vcsRepoUrl.text()), "fooo", self.ui.textBrowser)
        
    def stopCheckOut(self):        
        self.ui.checkOutStopBtn.setEnabled(False)
        self.ui.vcsListCBox.setFocus()
        
    def parseCommitLog(self):
        self.projCreate.listProjects1()        
        self.projCreate.exec_()        
        
    def train_testCsvGeneration(self):        
        self.projSelect.listProjects2()
        self.projSelect.exec_()

    def defect(self):
        gKeys = returnValuesforGKeys()
        generateKeys(gKeys[0], gKeys[1], gKeys[2], gKeys[3], gKeys[4], gKeys[5], self.ui.textBrowser)

        dMatch = returnValuesforDMatch()
        defectMatch(dMatch[0], dMatch[1], dMatch[2], dMatch[3], dMatch[4], dMatch[5], dMatch[6], self.ui.textBrowser)

        fMatch = returnValuesforFMatch()
        fileMatch(fMatch[0], fMatch[1], self.ui.textBrowser)
         
    def predict(self):

        prestProject = returnPathforProject()
        addProject(prestProject, self.ui.textBrowser)

        paths = returnPathforCsvFiles()
        convertToArff(paths[0], paths[1], paths[2], self.ui.textBrowser)

        predictWithPrest(self.ui.textBrowser)
